﻿Imports System.Net.Sockets
Imports System.Text
Imports iUtil
Public Class frmIRC
    Dim WithEvents wsocket As New SocketsClient

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        wsocket.Connect("127.0.0.1", 6667)

    End Sub
    Private Sub connected() Handles wsocket.onConnect
        wsocket.SendData(wsocket.StringToBytes("NICK intmed" & vbCrLf))
        wsocket.SendData(wsocket.StringToBytes("USER intmed2 localhost 0.0.0.0 :I am coded in VB.NET" & vbCrLf))
    End Sub

    Private Sub dataArrived(ByVal Data As Byte(), ByVal TotalBytes As Integer) Handles wsocket.onDataArrival
        Dim inData As String = wsocket.BytestoString(Data)
        AT(inData)
    End Sub

    Private Delegate Sub delAT(ByVal Text As String)
    Private Sub AT(ByVal Text As String)
        If txtOut.InvokeRequired Then
            Invoke(New delAT(AddressOf AT), New Object() {Text})
        Else
            Dim x As Integer
            Dim strdata() As String
            strdata = Text.Split(vbCrLf)
            For x = 0 To UBound(strdata) - 1
                MessageBox.Show(strdata(x))
                txtOut.AppendText(strdata(x) & vbCrLf)
            Next
            'txtOut.ScrollToCaret()
        End If

    End Sub

    Private Sub txtcmd_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtcmd.KeyPress
        If Microsoft.VisualBasic.Asc(e.KeyChar) = 13 Then
            wsocket.SendData(wsocket.StringToBytes(txtcmd.Text & vbCrLf))
            AT(txtcmd.Text)
        End If
    End Sub

End Class



''Dim winsock As System.Net.Sockets.Socket
'If tcpClient.Connected = False Then
'    tcpClient.Connect("127.0.0.1", 6667)
'End If

'Dim networkStream As NetworkStream = tcpClient.GetStream()
'If networkStream.CanWrite And networkStream.CanRead Then
'    ' Do a simple write.
'    Dim sendBytes As Byte() = Encoding.ASCII.GetBytes("NICK intmedvishnu" & vbCrLf & "USER intermediatech7@gmail.com 0 localhost :vishnu" & vbCrLf & "JOIN #windows" & vbCrLf)
'    'Dim sendBytes As Byte() = Encoding.ASCII.GetBytes()
'    networkStream.Write(sendBytes, 0, sendBytes.Length)
'    ' Read the NetworkStream into a byte buffer.
'    Dim bytes(tcpClient.ReceiveBufferSize) As Byte
'    networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
'    ' Output the data received from the host to the console.
'    Dim returndata As String = Encoding.ASCII.GetString(bytes)
'    'Console.WriteLine(("Host returned: " + returndata))
'    txtOut.Text += returndata & vbCrLf
'    MsgBox(returndata)
'Else
'    If Not networkStream.CanRead Then
'        MsgBox("cannot not write data to this stream")
'        'tcpClient.Close()
'    Else
'        If Not networkStream.CanWrite Then
'            MsgBox("cannot read data from this stream")
'            'tcpClient.Close()
'        End If
'    End If
'End If
'' pause so user can view the console output
''Console.ReadLine()